package com.shjn.squirrel.fragment;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.shjn.squirrel.utils.MeasureScreenUtils;

/**
 * 对话框基类
 */
public class BaseDialogFragment extends DialogFragment {

    //对话框布局资源文件
    private int mLayoutResource;
    //对话框宽度
    private int mWidth;
    //对话框高度
    private int mHeiht;
    //对话框高度位置偏离度
    private int mYBias;
    //布局视图
    protected View mView;
    //对话框
    protected AlertDialog mDialog;
    //对话框内容
    protected String mMessgae;
    //关闭对话框的回调接口对象
    protected OnCloseDialogListener mOnCloseDialogListener;

    /**
     * 关闭对话框的回调接口
     */
    public interface OnCloseDialogListener {

        void onCloseDialog();
    }

    /**
     * 设置关闭对话框的回调接口
     */
    public BaseDialogFragment setOnCloseDialogListener(OnCloseDialogListener closeDialogListener) {
        mOnCloseDialogListener = closeDialogListener;
        return this;
    }

    /**
     * 设置对话框提示内容
     *
     * @param message
     * @return
     */
    public BaseDialogFragment setMessage(String message) {
        mMessgae = message;
        return this;
    }

    /**
     * 设置布局资源
     *
     * @param layoutResource 资源文件
     */
    protected void setLayoutResource(int layoutResource) {
        mLayoutResource = layoutResource;
    }

    /**
     * 设置对话框宽和高
     *
     * @param width  对话框宽度，单位dp
     * @param height 对话框高度，单位dp
     * @param yBias  对话框高度偏移量，单位dp
     */
    protected void setdialogSize(int width, int height, int yBias) {
        mWidth = width;
        mHeiht = height;
        mYBias = yBias;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //设置对话框背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //生成对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        mView = LayoutInflater.from(getActivity()).inflate(mLayoutResource, null);
        builder.setView(mView);
        mDialog = builder.create();
        //设置对话框点击外部不会退出
        mDialog.setCanceledOnTouchOutside(false);
        //设置返回键对话框不退出
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                return true;
            }
        });
        return mDialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        Window dialogWindow = getDialog().getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
        //设置对话框位置
//        attributes.x =0;
        attributes.y = mYBias * (int) MeasureScreenUtils.getDensity(getActivity());
        //设置对话框大小
        attributes.width = mWidth * (int) MeasureScreenUtils.getDensity(getActivity());
        attributes.height = mHeiht * (int) MeasureScreenUtils.getDensity(getActivity());
        //设置对话框外部区域的透明度
        attributes.dimAmount = 0.3f;
        dialogWindow.setAttributes(attributes);
    }

}
